<?php
namespace frontend\controllers;

use Yii;
use common\models\Article;
use common\models\Game;
use common\models\GameWithType;
use common\models\GameVersion;
use yii\helpers\BaseJson;

/**
 * 文章控制器
 *
 * @author jois
 */
class ArticleController extends BaseController
{

    // 分类页 一页显示的条数
    public $limit = 10;

    public $ajax_limit = 8;

    /**
     * 文章分类页
     */
    public function actionNewsList()
    {
        $kind = Yii::$app->request->get('kind', Article::KIND_NEWS);
        // 每个类别取8条数据
        $news = Article::find()->with('game')
            ->where("kind=" . Article::KIND_NEWS)
            ->orderBy('id desc')
            ->limit($this->limit)
            ->all();
        $activity = Article::find()->with('game')
            ->where("kind=" . Article::KIND_ACTIVITY)
            ->orderBy('id desc')
            ->limit($this->limit)
            ->all();

        return $this->render('news-list', compact('kind', 'news', 'activity'));
    }

    /**
     * 文章分类页
     */
    public function actionActivityList()
    {
        $kind = Yii::$app->request->get('kind', Article::KIND_NEWS);
        // 每个类别取8条数据
        $news = Article::find()->with('game')
            ->where("kind=" . Article::KIND_NEWS)
            ->orderBy('id desc')
            ->limit($this->limit)
            ->all();
        $activity = Article::find()->with('game')
            ->where("kind=" . Article::KIND_ACTIVITY)
            ->orderBy('id desc')
            ->limit($this->limit)
            ->all();
        return $this->render('activity-list', compact('kind', 'news', 'activity'));
    }

    /**
     * 文章详细页
     */
    public function actionDetail()
    {
        $model = $prev = $prev = $about = $game = $game_version = $same_games = null;
        $id = Yii::$app->request->get('id', 0);
        if ((int) $id > 0) {
            // 本篇文章
            $model = Article::findOne((int) $id);
            $game=$game_version=null;
            // 本篇文章所属游戏的版本
            if (is_object($model->game)) {
                // $game = $model->game;
                $game = Game::find()->with('info')
                    ->where("game_id=" . $model->game_id)
                    ->with('types')
                    ->one();
                // 取is_first=1并且默认去mobileos_type_id=1[即安卓]的版本
                $game_version = $game->getVersions()
                    ->where("is_first=1")
                    ->orderBy('mobileos_type_id asc')
                    ->one();
            }

            // 上篇文章
            $prev = Article::find()->select("id,title")
                ->where("id<:id")
                ->addParams([
                'id' => (int) $id
            ])
                ->one();

            // 下篇文章
            $next = Article::find()->select("id,title")
                ->where("id>:id")
                ->addParams([
                'id' => (int) $id
            ])
                ->one();

            // 相关文章，为了性能加上select
            // TODO:随机抓取，后续请更改相关的算法或根据需求
            $about = Article::find()->select([
                'id',
                'kind',
                'title'
            ])
                ->orderBy("rand()")
                ->limit(5)
                ->all();

            // 同类游戏
            // TODO:随机抓取，后续请更改相关的算法或根据需求
            $type_ids = GameWithType::find()->select('type_id')
                ->where("game_id=" . $model->game_id)
                ->column();
            if (! empty($type_ids)) {
                $same_games = GameVersion::find()->where("is_first=1")
                    ->joinWith([
                    'game.types' => function ($query) use($type_ids)
                    {
                        $query->where([
                            "in",
                            "game_types.type_id",
                            $type_ids
                        ]);
                    }
                ])
                    ->orderBy("rand()")
                    ->limit(4)
                    ->all();
            }

            return $this->render('detail', compact('model', 'prev', 'next', 'about', 'game', 'game_version', 'same_games'));
        }
        return '文章不存在';
    }

    /**
     * 异步获取游戏列表
     */
    public function actionAjaxList()
    {
        // 验证page参数
        $page = (int) Yii::$app->request->get('page', 0);
        $kind = (int) Yii::$app->request->get('kind', '');
        if ($page > 1 && Yii::$app->request->isAjax) {
            // 开始获取数据
            if (empty($kind)) {
                $games = Article::find()->with('game')
                    ->addParams([
                    'kind' => $kind
                ])
                    ->orderBy("id desc")
                    ->offset(($page - 1) * $this->ajax_limit + 1)
                    ->limit($this->ajax_limit)
                    ->asArray()
                    ->all();
            } else {
                $games = Article::find()->with('game')
                    ->where("kind=:kind")
                    ->addParams([
                    'kind' => $kind
                ])
                    ->orderBy("id desc")
                    ->offset(($page - 1) * $this->ajax_limit + 1)
                    ->limit($this->ajax_limit)
                    ->asArray()
                    ->all();
            }
            return BaseJson::encode($games, true);
        }
        return BaseJson::encode('获取失败，参数或请求方法异常');
    }
}